package com.qc31.comlib.net



import com.qc31.baselib.utils.BuildConfigs
import com.qc31.baselib.utils.GsonUtil
import com.qc31.baselib.utils.Language
import com.qc31.comlib.UrlUtil
import com.qc31.comlib.UserConstant
import com.qc31.comlib.entity.*
import com.qc31.comlib.map.AppConfig
import com.qc31.comlib.map.AppGpsColumn
import io.reactivex.rxjava3.core.Single
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

/**
 *@Author pc_hfjs
 *@Date 2022/1/19 15:52
 *@Desc
 */
class NetRepository {
    private var api = Net.getService(ServiceApi::class.java)
    private var videoApi =
        Net.getServiceNoParams(ServiceApi::class.java, UrlUtil.mVideoHost)
    private var noParamApi = Net.getServiceNoParams(ServiceApi::class.java)

    //改变链接
    fun changeHost() {
        api = Net.getService(ServiceApi::class.java)
        noParamApi = Net.getServiceNoParams(ServiceApi::class.java)
    }

    //更新视频链接
    fun changeVideoHost() {
        videoApi = Net.getServiceNoParams(ServiceApi::class.java, UrlUtil.mVideoHost)
    }

    /**
     * 获取油耗统计数据
     */
    fun getOilStats(carId: String, sdate: String, edate: String): Single<OilDayBean> {
        return api.getOilStats(1, carId, sdate, edate)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<OilDayBean>(it.result.toString())
            }
    }

    /**
     * 获取油耗统计数据
     */
    fun getOilDayStats(typeId: String, carId: String, date: String): Single<OilDetail> {
        return api.getOilDayStats(1, typeId, carId, date)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<OilDetail>(it.result.toString())
            }
    }

    /**
     * 获取水位统计数据
     */
    fun getWaterStats(carId: String, sdate: String, edate: String): Single<OilDayBean> {
        return api.getOilStats(5, carId, sdate, edate)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<OilDayBean>(it.result.toString())
            }
    }

    /**
     * 获取水位统计数据
     */
    fun getWaterDayStats(typeId: String, carId: String, date: String): Single<OilDetail> {
        return api.getOilDayStats(5, typeId, carId, date)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<OilDetail>(it.result.toString())
            }
    }

    /**
     * 获取定位简易
     */
    fun getGpsR(
        teamId: String,
        carIds: String,
        simple: Boolean,
        filter: Boolean
    ): Single<ArrayList<GpsSimple>> {
        return api.getGpsR(teamId, carIds, simple, filter)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), GpsSimple::class.java)
            }
    }

    /**
     * 获取定位详情信息
     */
    fun getGpsRInfo(
        teamId: String,
        carIds: String,
        simple: Boolean = false,
        filter: Boolean = true
    ): Single<ArrayList<GpsDetail>> {
        return api.getGpsR(teamId, carIds, simple, filter)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), GpsDetail::class.java)
            }
    }


    /**
     * 历史轨迹
     */
    fun getHistoryLine(
        carId: String,
        startTime: String,
        endTime: String,
        maxSpeed: String,
        stopLong: String,
        filterInvalid: Boolean,
        filterAppend: Boolean,
        filterStop: Boolean
    ): Single<HistoryLine> =
        api.getHistoryGps(
            carId,
            startTime,
            endTime,
            maxSpeed,
            stopLong,
            filterInvalid,
            filterAppend,
            filterStop
        ).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<HistoryLine>(it.result.toString())
        }

    /**
     * 历史轨迹停车报警地点
     */
    fun getHistoryGpsAlarms(
        carId: String,
        startTime: String,
        endTime: String,
        maxSpeed: String,
        stopLong: String,
        filterInvalid: Boolean,
        filterAppend: Boolean,
        filterStop: Boolean,
        type: String,
    ): Single<HistoryGpsAlarm> =
        api.getHistoryGpsAlarms(
            carId,
            startTime,
            endTime,
            maxSpeed,
            stopLong,
            filterInvalid,
            filterAppend,
            filterStop,
            type
        ).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<HistoryGpsAlarm>(it.result.toString())
        }

    /**
     * 历史轨迹停车报警地点
     */
    fun getAllHisGps(
        carId: String,
        startTime: String,
        endTime: String,
        maxSpeed: String,
        stopLong: String,
        filterInvalid: Boolean,
        filterAppend: Boolean,
        filterStop: Boolean,
    ): Single<HistoryLine> =
        api.getAllHisGps(
            carId,
            startTime,
            endTime,
            maxSpeed,
            stopLong,
            filterInvalid,
            filterAppend,
            filterStop
        ).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<HistoryLine>(it.result.toString())
        }

    /**
     * 获取车辆信号线或提醒
     */
    fun getCarSgnRemind(carId: String, type: String): Single<SgnRemind> =
        api.getCarSgnRemind(carId, type).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<SgnRemind>(it.result.toString())
        }


    //查询版本信息
    fun getVersion(): Single<Version> =
        Net.getServiceParams(
            ServiceApi::class.java,
            UrlUtil.newBase(BuildConfigs.IP, BuildConfigs.PORT)
        ).getAppVer("android", "android")
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<Version>(it.result.toString())
            }


    /**
     *  登录
     */
    fun login(
        userId: String,
        pwd: String,
        loginType: String,
        plateColor: String = "",
        sms: Boolean = false,
        smsCode: String = ""
    ): Single<LoginEntity> =
        api.login(
            userId, pwd, loginType, plateColor,
            Language.instance.language, BuildConfigs.VERSION_NAME, sms, smsCode
        ).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<LoginEntity>(it.result.toString())
        }

    /**
     * 登出
     */
    fun logout(): Single<HBaseBean> =
        api.logout().compose(Net.applySingleSchedulers())

    /**
     * 字典
     */
    fun getDictionary(types: String): Single<Dictionary> =
        api.getDictionary(types, false, Language.instance.language)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<Dictionary>(it.result.toString())
            }

    /**
     * 链接获取车辆详情
     */
    fun getCarInfoQr(url: String): Single<QrCode> =
        noParamApi.getQrCarInfo(url).compose(Net.applySingleSchedulers())

    /**
     * 获取车辆详情
     */
    fun getCarInfo(carId: String): Single<CarInfo> =
        api.getCarInfo(carId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<CarInfo>(it.result.toString())
        }

    /**
     * 查询消息公告
     */
    fun getMessage(): Single<ArrayList<SystemMessage>> =
        api.getSysMsg().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), SystemMessage::class.java)
        }

    /******************************************车队车辆相关******************************************************/
    /**
     * 查询车队列表
     */
    fun getTeams(teamId: String = ""): Single<ArrayList<TeamInfo>> =
        api.getTeamList(teamId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), TeamInfo::class.java)
        }

    /**
     * 车队车辆逐级查询
     */
    fun getTeamCars(teamId: String = "0", video: Boolean): Single<TeamsCars> =
        api.getTeamCarList(teamId, video).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<TeamsCars>(it.result.toString())
        }

    /**
     * 车队下所有车辆
     */
    fun getTeamAllCars(teamId: String = "0"): Single<CarsInfo> =
        api.getTeamCars(teamId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<CarsInfo>(it.result.toString())
        }
//
//    //车辆列表 为空时请求全部车队
//    fun getCarList(
//        teamId: String = "",
//        carIds: String = "",
//        detail: Boolean = false
//    ): Single<CarInfoEntity> =
//        api.getCarList(teamId, carIds, detail).compose(Net.applySingleSchedulers())
//
    /**
     * 模糊查询车辆
     */
    fun getSearchCar(
        plate: String = "",
        type: String = "1",
        video: Boolean
    ): Single<ArrayList<CarLick>> =
        api.getCarsLike(plate, type, video).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), CarLick::class.java)
        }

    /**
     * 图例
     */
    fun getLegend(carId: String): Single<ArrayList<Legend>> =
        api.legend(carId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), Legend::class.java)
        }
    /***************************************************************************************************/

//    //实时视频  历史视频
//    fun getVideoState(url: String): Single<String> {
//        return videoApi.getVideoState(url)
//            .compose(Net.applyNoMapSingleSchedulers())
//    }
//
//    fun getState(url: String): Single<String> {
//        return videoApi.getState(url)
//            .compose(Net.applyNoMapSingleSchedulers())
//    }
//
    /*********************************      报表相关        ******************************************************/
    /**
     * 查询报警类型
     */
    fun getAlarmType(isAdas: Boolean): Single<ArrayList<AlarmType>> =
        api.getAlarmType(if (isAdas) 2 else 1).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), AlarmType::class.java)
        }


    /**
     * 查询报警历史
     */
    fun getHistoryAlarm(
        carId: String,
        startTime: String,
        endTime: String,
        typeIds: String,
        speed: String = "0",
        duration: String = "00:00:00"
    ): Single<HistoryAlarm> {


        return api.getHistoryAlarms(carId, startTime, endTime, typeIds, speed, duration)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<HistoryAlarm>(it.result.toString())
            }
    }


    /**
     * 当前报警统计
     */
    fun getAlarmCount(): Single<ArrayList<AlarmCount>> =
        api.getAlarmCount().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), AlarmCount::class.java)
        }

    /**
     * 查询报警详情
     */
    fun getAlarmDetail(alarmType: String): Single<ArrayList<AlarmDetail>> =
        api.getAlarmDetail(alarmType).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), AlarmDetail::class.java)
        }

    /**
     * 查询主动安全的多媒体数据
     */
    fun getAlarmMedia(
        carId: String,
        time: String,
        alarmType: String
    ): Single<ArrayList<AlarmMedia>> =
        api.getAlarmMedia(carId, time, alarmType).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), AlarmMedia::class.java)
        }

    //订阅报警
    fun setAlarmType(alarmIds: String): Single<HBaseBean> =
        api.setAlarms(alarmIds).compose(Net.applySingleSchedulers())

    /**
     * 根据时间查日里程
     */
    fun getDayMile(carId: String, startTime: String, endTime: String): Single<DayMile> =
        api.queryMile(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<DayMile>(it.result.toString())
        }
    /***************************************       ******************************************************/
    /**
     * 查询历史照片
     */
    fun getHistoryPhotos(
        carId: String,
        startTime: String,
        endTime: String
    ): Single<ArrayList<PhotoInfo>> =
        api.queryPhoto(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), PhotoInfo::class.java)
        }

    /**
     * 费用分析
     */
    fun getFeeDetail(
        carId: String,
        startTime: String,
        endTime: String
    ): Single<Fees> =
        api.feeStats(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<Fees>(it.result.toString())
        }

    /**
     * 费用各项详情
     */
    fun getFeeInfo(
        carId: String,
        feeType: String,
        startTime: String,
        endTime: String,
        driverName: String = ""
    ): Single<HBaseBean> =
        api.queryFeeInfo(carId, feeType, startTime, endTime, driverName)
            .compose(Net.applySingleSchedulers())

    /**
     * 整体数据
     */
    fun getAllData(): Single<AllData> =
        api.overallData().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<AllData>(it.result.toString())
        }

    /**停车统计*/
    fun getStopData(
        carId: String,
        startTime: String,
        endTime: String,
        duration: String,
        acc: Boolean,
    ): Single<StopStats> =
        api.stopStats(carId, startTime, endTime, duration, acc)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<StopStats>(it.result.toString())
            }

    /**
     * 获取模拟量设置信息
     */
    fun getMnConfig(carId: String, analogCode: String): Single<ArrayList<SensorCfg>> =
        api.sensorCfg(carId, analogCode).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), SensorCfg::class.java)
        }


    /**
     * 模拟量统计
     */
    fun getAnalogStats(
        carId: String,
        startTime: String,
        endTime: String,
        analogCode: String,
        mnIndex: String
    ): Single<ArrayList<SensorInfo>> =
        api.sensorStats(carId, startTime, endTime, analogCode, mnIndex)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), SensorInfo::class.java)
            }

    /**
     * 提醒查询
     */
    fun getRemindData(teamId: String, type: String): Single<ArrayList<RemindInfo>> =
        api.queryReminds(teamId, type).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), RemindInfo::class.java)
        }

    /**
     * 整点位置
     */
    fun getPointsData(carId: String, date: String): Single<ArrayList<HourPoint>> =
        api.queryHourPoint(carId, date).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), HourPoint::class.java)
        }

    /**
     * ACC状态
     */
    fun getAccStatsData(carId: String, startTime: String, endTime: String): Single<AccStats> =
        api.accStats(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<AccStats>(it.result.toString())
        }

    /**
     * 车辆登退签
     */
    fun getCarSignInOut(carId: String, startTime: String, endTime: String): Single<CarSign> =
        api.querySignCar(carId, startTime, endTime)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<CarSign>(it.result.toString())
            }

    /**
     * 司机登退签
     */
    fun getDriverSignInOut(
        driverId: String,
        startTime: String,
        endTime: String
    ): Single<DriverSign> =
        api.querySignDriver(driverId, startTime, endTime)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<DriverSign>(it.result.toString())
            }

    /**
     * 获取司机信息
     */
    fun getDriversData(): Single<ArrayList<DriverInfo>> =
        api.queryDriver().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), DriverInfo::class.java)
        }

    /**
     * 流量统计
     */
    fun getFlowStatsData(
        carId: String,
        startTime: String,
        endTime: String
    ): Single<ArrayList<FlowInfo>> =
        api.flowStats(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), FlowInfo::class.java)
        }

    /**
     * 视频流量统计
     */
    fun getVideosStats(
        carId: String,
        startTime: String,
        endTime: String
    ): Single<ArrayList<FlowInfo>> =
        api.videoStats(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), FlowInfo::class.java)
        }

    /**
     * 查岗历史
     */
    fun getCheckHistory(
        startTime: String,
        endTime: String
    ): Single<ArrayList<CheckInfo>> =
        api.queryCheck(startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), CheckInfo::class.java)
        }

    /**
     * 保险查询
     */
    fun getInsureData(
        carId: String,
        startTime: String,
        endTime: String
    ): Single<ArrayList<InsureInfo>> =
        api.getInsurance(carId, startTime, endTime).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), InsureInfo::class.java)
        }

    /**
     * 证件查询
     */
    fun getCardData(
        carId: String,
        startTime: String,
        endTime: String,
        type: String
    ): Single<ArrayList<CardInfo>> = api.getCertificate(carId, startTime, endTime, type)
        .compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), CardInfo::class.java)
        }

    /**
     * 查询日志相关
     */
    fun getLogOpera(
        action: String,
        carId: String,
        cmdId: String,
        typeId: String,
        startTime: String,
        endTime: String,
        opera: String
    ): Single<ArrayList<CmdLog>> =
        api.queryLog(action, startTime, endTime, typeId, opera, cmdId, carId)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), CmdLog::class.java)
            }

    /**
     * 查询指令
     */
    fun getCmdData(): Single<ArrayList<CmdInfo>> =
        api.cmdList().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), CmdInfo::class.java)
        }

    /**
     * 获取指令类型
     */
    fun getLogData(): Single<ArrayList<LogTypeInfo>> =
        api.logType().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), LogTypeInfo::class.java)
        }

    /**
     * 区域统计
     */
    fun getRegionCars(regionId: String): Single<ArrayList<RegionStats>> =
        api.getRegionCars(regionId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), RegionStats::class.java)
        }

    /**
     * 获取权限下所有区域(不含经纬度) 0-目录  1-区域
     */
    fun getRegions(type: String = "1"): Single<ArrayList<RegionInfo>> =
        api.getRegions(type).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), RegionInfo::class.java)
        }

    /**
     * 区域查车
     */
    fun queryRegion(
        carId: String,
        startTime: String,
        endTime: String,
        regions: String
    ): Single<RegionCar> =
        api.getCarRegions(carId, startTime, endTime, regions)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<RegionCar>(it.result.toString())
            }

    /**
     * 离线统计
     */
    fun getOfflineData(
        objType: String,
        objIds: String,
        queryType: String,
        offLineType: String,
        offLineDays: String
    ): Single<ArrayList<OfflineInfo>> =
        api.offlineStats(objType, objIds, queryType, offLineType, offLineDays)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), OfflineInfo::class.java)
            }

    /**
     *  欠费统计信息 （运营状况）
     */
    fun getOperationData(teamId: String, days: Int): Single<ArrayList<ArrearsInfo>> =
        api.getArrearsInfo(teamId, days).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), ArrearsInfo::class.java)
        }

    /**********************************************    Other 其它   *******************************************/
    //添加费用
    fun updateFee(
        feeType: String,
        entity: String,
        action: String = "add"
    ): Single<HBaseBean> =
        api.saveFee(feeType, action, entity).compose(Net.applySingleSchedulers())

    //添加费用
    fun updateImgFee(
        feeType: String,
        entity: String,
        files: List<MultipartBody.Part?>?,
        action: String = "add"
    ): Single<HBaseBean> =
        noParamApi.saveFee(UserConstant.sessionId, feeType, action, entity, files)
            .compose(Net.applySingleSchedulers())

    /**
     * 新增车辆
     */
    fun addCar(
        plate: String, plateColor: String, simCard: String, tmnNo: String,
        tmnKey: String, teamId: String, keyType: String,
        channelCount: String, carPhoto: MultipartBody.Part?, tmnPhoto: MultipartBody.Part?
    ): Single<HBaseBean> = noParamApi.addCar(
        UserConstant.sessionId,
        plate, plateColor, simCard, tmnNo, tmnKey, teamId,
        keyType, channelCount, "", carPhoto, tmnPhoto
    ).compose(Net.applySingleSchedulers())

    /**
     * 新增车辆
     */
    fun addCar2(
        plate: String, plateColor: String, simCard: String, tmnNo: String,
        tmnKey: String, teamId: String, keyType: String,
        channelCount: String, carPhoto: MultipartBody.Part?, tmnPhoto: MultipartBody.Part?
    ): Single<HBaseBean> {
        val map = HashMap<String, RequestBody?>()
        map["sessionId"] = getResponseBody(UserConstant.sessionId)
        map["plate"] = getResponseBody(plate)
        map["plateColor"] = getResponseBody(plateColor)
        map["simCard"] = getResponseBody(simCard)
        map["tmnNo"] = getResponseBody(tmnNo)
        map["tmnKey"] = getResponseBody(tmnKey)
        map["teamId"] = getResponseBody(teamId)
        map["keyType"] = getResponseBody(keyType)
        map["channelCount"] = getResponseBody(channelCount)
        return noParamApi.addCar2(
            map, carPhoto, tmnPhoto
        ).compose(Net.applySingleSchedulers())
    }

    private fun getResponseBody(value: String): RequestBody? {
        return value.toRequestBody("text/plain".toMediaTypeOrNull())
    }

    //指令下发
    fun sendCmd(
        userId: String,
        carId: String,
        cmdId: String,
        cmd: String,
        params: String
    ): Single<CmdReply> =
        api.sendCmd(userId, carId, cmdId, cmd, params)
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formObject<CmdReply>(it.result.toString())
            }

    /**
     * 获取短信指令
     */
    fun getSMSCmd(): Single<ArrayList<SmsCmdInfo>> =
        api.smsCmd().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), SmsCmdInfo::class.java)
        }

    /**
     * 获取维修列表
     */
    fun getRepairs(): Single<ArrayList<RepairInfo>> =
        api.getRepairList().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), RepairInfo::class.java)
        }

    //维修回复提交
    fun updateRepair(modifyId: String, desc: String): Single<HBaseBean> =
        api.replyRepair(modifyId, desc).compose(Net.applySingleSchedulers())

    //维修申报
    fun submitRepair(
        carId: String,
        tickName: String,
        tickPhone: String,
        tickDesc: String,
        files: List<MultipartBody.Part?>?
    ): Single<HBaseBean> =
        noParamApi.addRepair(
            UserConstant.sessionId,
            carId,
            tickName,
            tickPhone,
            tickDesc,
            "",
            files
        )
            .compose(Net.applySingleSchedulers())

    /*****************************************   Menu 菜单    ****************************************************/
    /**登录历史
     *
     */
    fun getLoginHistory(): Single<ArrayList<HistoryLogin>> =
        api.loginHistory().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), HistoryLogin::class.java)
        }

    /**
     * 关于我们
     */
    fun aboutUs(): Single<AboutUs> =
        api.aboutUs().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<AboutUs>(it.result.toString())
        }

    /**
     * 修改密码
     */
    fun upPwd(newPwd: String, pwdConvert: Boolean): Single<HBaseBean> =
        api.uptPwd(newPwd, pwdConvert).compose(Net.applySingleSchedulers())

    /**
     * 获取车辆相关照片
     */
    fun getPic(carId: String, type: String): Single<ArrayList<String>> =
        api.carPhoto(carId, type).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), String::class.java)
        }

    /**
     * 更新接口
     */
    fun getUpdateInfo(url: String): Single<UpdateEntity> =
        noParamApi.getUpdate(url).compose(Net.applySingleSchedulers())

    /**
     * 欠费通知
     */
    fun getArrearsInfo(): Single<ArrearsNotice> =
        api.arrearsNotice().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<ArrearsNotice>(it.result.toString())
        }

    /**
     * 车辆修改
     */
    fun modifyCar(
        oldCarId: String,
        plate: String,
        carName: String,
        plateColor: String,
        teamId: String,
        channelCount: String
    ): Single<HBaseBean> =
        api.modifyCar(oldCarId, plate, carName, plateColor, teamId, channelCount)
            .compose(Net.applySingleSchedulers())

    /**
     * 获取常用文本信息
     */
    fun getCmdNote(): Single<CmdNote> =
        api.cmdNote().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<CmdNote>(it.result.toString())
        }

    /**
     * 手机登录前状态接口
     */
    fun getLoginPre(): Single<LoginPre> =
        api.loginPre().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<LoginPre>(it.result.toString())
        }

    /**
     * 手机登录发送验证码
     */
    fun sendSms(userId: String, loginType: String, plateColor: String): Single<HBaseBean> =
        api.sendSms(userId, loginType, plateColor, Language.instance.language).compose(Net.applySingleSchedulers())

    /**
     * 获取设备异常
     */
    fun getDeviceException(): Single<ArrayList<DeviceExc>> =
        api.deviceExc("1", "").compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), DeviceExc::class.java)
        }

    /**
     * 获取设备异常详情
     */
    fun getDeviceInfo(type: String, name: String): Single<ArrayList<DeviceInfo>> =
        api.deviceExc(type, name).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), DeviceInfo::class.java)
        }

    /**
     *  联系我们
     */
    fun getCmpLink(): Single<ArrayList<ContactUs>> =
        api.contactUs().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), ContactUs::class.java)
        }

    /**
     * 车辆周边
     */
    fun getCarAround(carId: String, radius: String): Single<ArrayList<CarAround>> =
        api.carAround(carId, radius).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), CarAround::class.java)
        }

    /*********************************    视频   ***************************************************/
    //实时视频  历史视频
    fun getVideoState(url: String): Single<String> {
        return videoApi.getVideoState(url)
            .compose(Net.applyNoMapSingleSchedulers())
    }


    fun getState(url: String): Single<String> {
        return videoApi.getState(url)
            .compose(Net.applyNoMapSingleSchedulers())
    }

    /*********************************  个推推送  **********************************************/
    /**
     * 推送开关接口
     */
    fun pushAlarm(isPush: Boolean, clientId: String): Single<HBaseBean> =
        api.push(isPush, clientId).compose(Net.applySingleSchedulers())
    /*********************************  维修费用  **********************************************/
    /**
     * 维修费用添加照片
     */
    fun upPhoto(addfile: String, tmnPhoto: MultipartBody.Part?): Single<HBaseBean> =
        noParamApi.upPhoto(UserConstant.sessionId, addfile, tmnPhoto)
            .compose(Net.applySingleSchedulers())
    /*********************************  微信分享  **********************************************/
    /**
     * 微信分享实时定位
     */
    fun shareLocation(carId: String): Single<HBaseBean> =
        api.shareLocation(carId, "1").compose(Net.applySingleSchedulers())

    /**
     * 微信分享历史轨迹
     */
    fun shareLine(
        carId: String, stime: String, etime: String,
        filter0: String, filterInvalid: String, filterAppend: String
    ): Single<HBaseBean> =
        api.shareLine(carId, "h1", stime, etime, filter0, filterInvalid, filterAppend)
            .compose(Net.applySingleSchedulers())

    /** 获取权限下所有标注 */
    fun getAllPoi(): Single<ArrayList<LocationPoi>> =
        api.getPoiS().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), LocationPoi::class.java)
        }

    /**
     * 通过地址查端口
     */
    fun getPortByAddr(urlHost: String, addr: String): Single<ArrayList<String>> =
        noParamApi.getPortByAddr("http://$urlHost:9999/api/plat/getPortByAddress?address=$addr")
            .compose(Net.applySingleSchedulers()).map {
                GsonUtil.formArrayList(it.result.toString(), String::class.java)
            }

    /**
     * 获取用户配置
     */
    fun getAppConfig(): Single<AppConfig> =
        api.getAppCfg().compose(Net.applySingleSchedulers()).map {
            GsonUtil.formObject<AppConfig>(it.result.toString())
        }
    /**
     * 保存用户设置
     */
    fun saveAppConfig(key: String, data: String): Single<HBaseBean> =
        api.saveAppCfg( key, data).compose(Net.applySingleSchedulers())

    /**
     * 获取单车配置
     */
    fun getCarCfg(carId:String): Single<ArrayList<AppGpsColumn>> =
        api.getCarCfg(carId).compose(Net.applySingleSchedulers()).map {
            GsonUtil.formArrayList(it.result.toString(), AppGpsColumn::class.java)
        }
    /**
     * 保存单车设置
     */
    fun saveCarCfg(carids: String, data: String): Single<HBaseBean> =
        api.saveCarCfg( carids, data).compose(Net.applySingleSchedulers())

}